import json
from json import JSONEncoder
from time import time

from sqlalchemy.orm import class_mapper

from api.entity import TaskEntity


class MyEncoder(JSONEncoder):
    def default(self, obj):
        if isinstance(obj, time):
            return int(obj)
        else:
            return super(MyEncoder, self).default(obj)

    @staticmethod
    def classToDict(obj):
        is_list = obj.__class__ == [].__class__
        is_set = obj.__class__ == set().__class__

        if is_list or is_set:
            obj_arr = []
            for o in obj:
                dict = {}
                dict.update(o.__dict__)
                obj_arr.append(dict)
            return obj_arr
        else:
            dict = {}
            dict.update(obj.__dict__)
            return dict